package com.zz.bsea.order.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import com.zz.bsea.order.dto.OrderDTO;
import com.zz.bsea.order.entity.OrderDetail;
import com.zz.bsea.order.entity.OrderMaster;

public class OrderService {
	//返回主表的ID
	public int add(OrderDTO orderdto ) {
		Random random = new Random();
	    int id = random.nextInt(9000) + 1000;
	    OrderMaster master=new OrderMaster();
	    master.setId(id);
	    master.setUserName(orderdto.getUserName());
	    master.setAddress(orderdto.getAddress());
	    HashSet<OrderDetail> set=orderdto.getOrderDetailset();
	    set.forEach(d->{
	    	//这句话保证了主表的ID和详情表里面主表ID的字段值保持一样
	    	d.setMasterId(id);
	    	addDetail(d);
	    });
	    
		return id;
	}
	
	//查询订单所有数据
	public List<OrderDTO> getAll(){
		List<OrderDTO> ls0=new ArrayList();
		List<OrderMaster> ls1=getAllOrderMaster();
		ls1.forEach(master->{
			OrderDTO dto=new OrderDTO();
			dto.setId(master.getId());
			dto.setAddress(master.getAddress());
			//....
			HashSet<OrderDetail> detailset=getAllOrderDetail(master.getId());
			dto.setOrderDetailset(detailset);
			
			ls0.add(dto);
			
		});
		return ls0;
	}
	
	
	public List<OrderMaster> getAllOrderMaster() {
		List<OrderMaster> ls=new ArrayList();
		//  select * from ordermaster;
		return ls;
	}
	
	public HashSet<OrderDetail> getAllOrderDetail(int mastId) {
		HashSet<OrderDetail> ls=new HashSet();
		//  select * from orderdetail where masterid=?;
		return ls;
	}
	
	
	public boolean addMaster( OrderMaster master) {
		boolean f=true;
		//jdbc 存数据到master表
		return f;
	}
	
	public boolean addDetail( OrderDetail detail) {
		boolean f=true;
		//jdbc 存数据到detail表
		return f;
	}
	
	

}
